package splash.core.execution;

import java.awt.Robot;
import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.PointerInfo;
import java.awt.event.InputEvent;

import java.net.Socket;
import java.net.ServerSocket;

import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MouseThread implements Runnable {
		
        private final int MOUSE_SPEED = 5; 

        private ServerSocket s;
        private Robot r;
        
        private MouseThread()
        {
        }
		
        public MouseThread(ServerSocket s, Robot r)
        {
            this.s = s;
            this.r = r;
        }

        //@Override
        public void run()
        {
            Socket sck = null;
            InputStream is = null;
            
            try
            {
                s.setSoTimeout(20000);
                sck = s.accept();
                is = sck.getInputStream();
            }
            catch(IOException e)
            {
                e.printStackTrace();
                close();
                return;
            }
            
            int bajt,x,y;
            
            while(true)
            {
            	try
            	{
                    bajt = is.read();
            	}
            	catch(IOException e)
            	{
                    return;
            	}
            	
            	if(bajt == -1)
            		break;
            	 
            	/* Get position */
            	PointerInfo pi = MouseInfo.getPointerInfo();
            	x = (int)pi.getLocation().getX();
            	y = (int)pi.getLocation().getY();
            	
            	switch(bajt)
            	{
                    case 'u':
                            r.mouseMove(x, y - MOUSE_SPEED);
                            break;
                    case 'd':
                            r.mouseMove(x, y + MOUSE_SPEED);
                            break;
                    case 'l':
                            r.mouseMove(x - MOUSE_SPEED, y);
                            break;
                    case 'r':
                            r.mouseMove(x + MOUSE_SPEED, y);
                            break;
                    /* left {x} and right {y} click */
                    case 'x':
                            r.mousePress(InputEvent.BUTTON1_MASK);
                            r.mouseRelease(InputEvent.BUTTON1_MASK);
                            break;
                    case 'y':
                            r.mousePress(InputEvent.BUTTON2_DOWN_MASK);
                            r.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
                            break;
                    /* scroll up {w} and down {s} */
                    case 'w':
                            r.mouseWheel(-MOUSE_SPEED);
                            break;
                    case 's':
                            r.mouseWheel(MOUSE_SPEED);
                            break;
            	}
            }	
        }
        
        private void close()
        {
            try
            {
                if(s != null)
                    s.close();
            }
            catch(IOException ioe)
            {
                s = null;
                System.gc();
            }
        }
}
